package Classes.GameViews.ViewCartoonTargets// ActionScript file
{
	import Classes.CustomEvents.CustomEvent;
	import Classes.Interfaces.I_NeedSWFSource;
	import Classes.Managers.M_BaseCartoons;
	import Classes.Managers.M_ConfigData;
	import Classes.Managers.M_LoadSWFSource;
	import Classes.Managers.M_PaperManCartoons;
	
	import MV_BMP.BMPMCEvent;
	import MV_BMP.MovieClip_BMP;
	
	import PaperMan.PaperManComponent;
	
	import flash.geom.Point;
	
	/**
	 *换装纸娃娃类 
	 * @author 葛增文
	 * 
	 */	
	public class VCT_PaperMan extends ViewCartoonTarget implements I_NeedSWFSource
	{
		public static const PAPERMANDRESSESFOLDERNAME:String=M_ConfigData.instance.imgUrl;
		
		private var _baseCartoonType:String;
		private var _dressStateObj:Object;
		private var _nowActionName:String;
		private var _nowDir:String;
		public function VCT_PaperMan(baseCartoonType:String="character_boy1"):void
		{
			/*this._baseCartoonType=baseCartoonType;
			this.init();*/
			this.addListener();
			this.setBaseCartoonType(baseCartoonType);
		}
		
		public function setBaseCartoonType(baseCartoonType:String):void
		{
			if(this.baseCartoonType==baseCartoonType)return;
			this._baseCartoonType=baseCartoonType;
			var baseCartoonObj:Object=M_BaseCartoons.instance.getBaseCartoonObjByType(this.baseCartoonType);
			if(!baseCartoonObj.hasCartoon)throw new Error("模型尚未载入");
			var creatObj:Object=new Object;
			for(var actionName:String in baseCartoonObj.actions)
			{
				var actionObj:Object=baseCartoonObj.actions[actionName];
				var creatPaperMan:PaperManComponent=new PaperManComponent();
				creatPaperMan.scaleMotionX=ViewCartoonTarget.scaleValue;
				creatPaperMan.scaleMotionY=ViewCartoonTarget.scaleValue;
				creatPaperMan.motionName=actionObj.className;
				var dressState:Object=new Object;
				for(var i:int=0;i<40;i++)
				{
					dressState["part"+i+"Str"]="default";
				}
				creatPaperMan.drawRate=-1;
				creatPaperMan.drawNow();
				creatObj[actionName]={cartoon:creatPaperMan,setX:actionObj.setX*ViewCartoonTarget.scaleValue,setY:actionObj.setY*ViewCartoonTarget.scaleValue,dressState:dressState};
			}
			this.setCartoons(creatObj);
		}
		
		private function addListener():void
		{
			this.addEventListener(BMPMCEvent.LAST_FRAME,lastFrameHandler);
		}
		
		private function removeListener():void
		{
			this.removeEventListener(BMPMCEvent.LAST_FRAME,lastFrameHandler);
		}
		
		private function lastFrameHandler(e:BMPMCEvent):void
		{
			if(this.nowActionName=="attack" ||this.nowActionName=="attack_throw"||this.nowActionName=="attack_short"|| this.nowActionName=="beHitted" || this.nowActionName=="heal" || this.nowActionName=="collect")
			{
				this.dispatchEvent(new CustomEvent(CustomEvent.CARTOONPLAYOVER,this.nowActionName,true))
			}
		}
		
		/**
		 *更新衣服 
		 * 
		 */		
		private function updateSuits():void
		{
			for each(var cartoonObj:Object in this.cartoonsObj)
			{
				for(var dressPart:String in cartoonObj.dressState)
				{
					(cartoonObj.cartoon as PaperManComponent)[dressPart]=cartoonObj.dressState[dressPart];
				}
			}
		}
		
		/**
		 *穿上某件衣服 
		 * @param suitName
		 * 
		 */		
		public function dress(suitName:String):void
		{
			var suitSetObj:Object=M_PaperManCartoons.instance.getSuitSetObjByName(suitName);
			for(var actionName:String in suitSetObj.suitSkins)
			{
				var suitSkinObj:Object=suitSetObj.suitSkins[actionName];
				var dressState:Object=this.cartoonsObj[actionName].dressState;
				for(var i:int=0;i<40;i++)
				{
					var partName:String="part"+i+"Str";
					var suitPartSet:String=suitSkinObj[partName];
					if(suitPartSet!="")
					{
						dressState[partName]=suitPartSet;
					}
				}
			}
			if(M_LoadSWFSource.instance.checkSource(suitName))
			{
				this.updateSuits();
			}
			else
			{
				var sourceName:String=suitSetObj.skinsSource;
				var sourcePath:String=PAPERMANDRESSESFOLDERNAME+sourceName;
				M_LoadSWFSource.instance.needLoad(this,sourcePath,sourceName);
			}
			
		}
		
		/**
		 *脱掉某件衣服 
		 * @param suitName
		 * 
		 */		
		public function undress(suitName:String):void
		{
			var suitSetObj:Object=M_PaperManCartoons.instance.getSuitSetObjByName(suitName);
			for(var actionName:String in suitSetObj.suitSkins)
			{
				var suitSkinObj:Object=suitSetObj.suitSkins[actionName];
				var dressState:Object=this.cartoonsObj[actionName].dressState;
				for(var i:int=0;i<40;i++)
				{
					var partName:String="part"+i+"Str";
					var suitPartSet:String=suitSkinObj[partName];
					if(suitPartSet!="")
					{
						dressState[partName]="default"
					}
				}
			}
			this.updateSuits();
		}
		
		/**
		 * 人物碰撞检测 
		 * @param point
		 * @return 
		 */		
		public function hitTest(point:Point):Boolean
		{
			var checkTarget:MovieClip_BMP=this.getChildAt(0) as MovieClip_BMP;
			if(checkTarget==null)return false;
			if(this.nowDir=="leftUp" || this.nowDir=="rightDown")
			{
				point.x+=checkTarget.width;
			}
			return checkTarget.hitTest(point);
		}
		
		/**
		 *脱掉所有衣服 
		 * 
		 */		
		public function undressAll():void
		{
			for each(var cartoonObj:Object in this.cartoonsObj)
			{
				var dressState:Object=cartoonObj.dressState;
				for(var i:int=0;i<40;i++)
				{
					dressState["part"+i+"Str"]="default";
				}
			}
			this.updateSuits();
		}
		
		public function sourceLoadOver(sourceName:String):void
		{
			this.updateSuits();
		}
		
		/**
		 *播放行为 
		 * @param dir
		 * @param actionName
		 * 
		 */		
		public function playAction(dir:String,actionName:String):void
		{
			switch(dir)
			{
				case "up":
					dir="leftUp";
					break;
				case "right":
					dir="rightDown";
					break;
				case "down":
					dir="rightDown";
					break;
				case "left":
					dir="leftDown";
					break;
			}
			if(this.nowDir==dir && this.nowActionName==actionName)return;
			var isMirror:Boolean=false;
			this._nowDir=dir;
			this._nowActionName=actionName;
			if(dir=="leftUp")
			{
				isMirror=true;
				dir="rightUp";
			}
			else if(dir=="rightDown")
			{
				isMirror=true;
				dir="leftDown";
			}
			this.playCartoon(actionName+"_"+dir,isMirror);
		}
		
		override public function release():void
		{
			this.removeListener();
			super.release();
			this._dressStateObj=null;
		}
		
		/*getter & setter*/
		public function get baseCartoonType():String
		{
			return _baseCartoonType;
		}
		
		public function get dressStateObj():Object
		{
			return _dressStateObj;
		}
		
		public function get nowActionName():String
		{
			return _nowActionName;
		}
		
		public function get nowDir():String
		{
			return _nowDir;
		}
		
		
	}
}